﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ServerNodeGui
{
	public partial class NodeDetailsDialog : Form
	{
		public NodeDetailsDialog()
		{
			InitializeComponent();
			AcceptNewJobs = true;
			NodeName = String.Empty;
		}

		public String NodeName;
		public int ShortTimeout;
		public int LongTimeout;
		public Boolean AcceptNewJobs;
		public Guid NodeId;
		public int QueueLength;

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			txtNodeName.Text = NodeName;
			txtLongTimeout.Text = LongTimeout.ToString();
			txtShortTimeout.Text = ShortTimeout.ToString();
			checkAcceptJobs.Checked = AcceptNewJobs;
			txtQueueLength.Text = QueueLength.ToString();

			if (NodeId.Equals(Guid.Empty) == false)
			{
				this.Text += " " + NodeId.ToString();
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			try
			{
				if (String.IsNullOrEmpty(txtNodeName.Text))
					throw new ArgumentException("Node Name cannot be empty");

				NodeName = txtNodeName.Text;

				int i = 0;
				if (Int32.TryParse(txtLongTimeout.Text, out i) == false)
					throw new ArgumentException("Error timeout must be a valid integer");
				LongTimeout = i;

				if (Int32.TryParse(txtShortTimeout.Text, out i) == false)
					throw new ArgumentException("Normal timeout must be a valid integer");
				ShortTimeout = i;

				AcceptNewJobs = checkAcceptJobs.Checked;

				this.DialogResult = DialogResult.OK;
				this.Close();
			}
			catch (ArgumentException ex)
			{
				lblErrorMessage.Text = "Error: " + ex.Message;
			}
			catch (Exception)
			{
				return;
			}
		}
	}
}
